package com.deer.wms.base.system.web;

import com.deer.wms.base.system.model.BoxInfo;
import com.deer.wms.base.system.model.BoxInfoCriteria;
import com.deer.wms.base.system.service.BoxInfoService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.system.manage.constant.SystemManageConstant;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Created by  on 2021/03/17.
 * Modified by wei on 2021/10/12.
 */
@RestController
@RequestMapping("/box/infos")
public class BoxInfoController {

    @Autowired
    private BoxInfoService boxInfoService;

    @Autowired
    private CodeRuleService codeRuleService;

    @PostMapping("/add")
    public Result add(@RequestBody BoxInfo boxInfo) {

        BoxInfoCriteria boxInfoCriteria = new BoxInfoCriteria();
        boxInfoCriteria.setWareId(boxInfo.getWareId());
        boxInfoCriteria.setBoxCode(boxInfo.getBoxCode());
        List<BoxInfo> list = boxInfoService.findList(boxInfoCriteria);
        if (list.size() > 0) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "编码重复");
        }
        boxInfoService.save(boxInfo);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        boxInfoService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody BoxInfo boxInfo) {
        boxInfoService.updateById(boxInfo);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        BoxInfo boxInfo = boxInfoService.getById(id);
        return ResultGenerator.genSuccessResult(boxInfo);
    }

    @GetMapping("/list")
    public Result list(BoxInfoCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BoxInfo> list = boxInfoService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "后台生成容器编码")
    @GetMapping("/generate")
    public Result generateBox(@RequestParam Integer count){
        String s = codeRuleService.generateCellCodeRedis();
        ;
        return ResultGenerator.genSuccessResult(Arrays.asList(s));
    }

}
